package com.woniuxy.oldmanapi.controller;

import com.github.pagehelper.PageInfo;
import com.woniuxy.oldmanapi.entity.Recipe;
import com.woniuxy.oldmanapi.entity.Recipecategory;
import com.woniuxy.oldmanapi.service.FoodService;
import com.woniuxy.oldmanapi.service.RecipeService;
import com.woniuxy.oldmanapi.vo.ResponseResult;
import com.woniuxy.oldmanapi.yvo.RecipeVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class RecipeController {
    @Autowired
    RecipeService recipeService;
    @Autowired
    FoodService foodService;

    @GetMapping("/recipe/list")
    public ResponseResult<PageInfo<RecipeVo>> initSelectByCons(@RequestParam(defaultValue = "-1") Integer goryId,
                                                               String name,
                                                               Integer now,
                                                               Integer size) {
        ResponseResult<PageInfo<RecipeVo>> result = new ResponseResult<>();
        try {
            PageInfo<RecipeVo> pageInfo = recipeService.initSelectByCons(goryId, name, now, size);
            if (pageInfo == null) {
                result.setCode(204);
                result.setMsg("无食谱,请先添加");
            } else {
                result.setCode(200);
                result.setMsg("查询成功");
                result.setData(pageInfo);
            }
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode(201);
            result.setMsg("系统繁忙,请稍后再试");
        }
        return result;
    }

    @GetMapping("/recipe/selectRecipeById/{id}")
    public ResponseResult<Recipe> selectRecipeById(@PathVariable("id") Integer id) {
        ResponseResult<Recipe> result = new ResponseResult<>();
        try {
            Recipe recipe = recipeService.selectRecipeById(id);
            result.setCode(200);
            result.setMsg("查询成功");
            result.setData(recipe);
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode(201);
            result.setMsg("系统繁忙,请稍后再试");
        }
        return result;
    }

    @PostMapping("/recipe/updateRecipe")
    public ResponseResult<Recipe> updateRecipe(@RequestBody Recipe recipe) {
        ResponseResult<Recipe> result = new ResponseResult<>();
        try {
            recipeService.updateRecipe(recipe);
            Recipe recipe1 = recipeService.selectRecipeById(recipe.getId());
            result.setCode(200);
            result.setData(recipe1);
            result.setMsg("修改成功");
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode(201);
            result.setMsg("系统繁忙,请稍后再试");
        }
        return result;
    }
    @DeleteMapping("/recipe/deleteFoodByRecipeId/{foodId}/{recipeId}")
    public ResponseResult<RecipeVo> deleteFoodByRecipeId(@PathVariable("foodId") Integer foodId,@PathVariable("recipeId") Integer recipeId) {
        ResponseResult<RecipeVo> result = new ResponseResult<>();
        try {
            foodService.deleteFoodByRecipeId(foodId,recipeId);
            RecipeVo recipe1 = recipeService.SelectRecipeVoByRecipeId(recipeId);
            result.setCode(200);
            result.setData(recipe1);
            result.setMsg("修改成功");
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode(201);
            result.setMsg("系统繁忙,请稍后再试");
        }
        return result;
    }
    @DeleteMapping("/recipe/deleteRecipe/{recipeId}")
    public ResponseResult<Void> deleteRecipe(@PathVariable("recipeId") Integer recipeId) {
        ResponseResult<Void> result = new ResponseResult<>();
        try {
            foodService.deleteAllFoodByRecipeId(recipeId);
            recipeService.deleteRecipe(recipeId);
            result.setCode(200);
            result.setMsg("删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode(201);
            result.setMsg("系统繁忙,请稍后再试");
        }
        return result;
    }
    @PostMapping("/recipe/addFoodByRecipeId/{foodId}/{recipeId}")
    public ResponseResult<RecipeVo> addFoodByRecipeId(@PathVariable("foodId") Integer foodId,@PathVariable("recipeId") Integer recipeId) {
        ResponseResult<RecipeVo> result = new ResponseResult<>();
        try {
            foodService.addFoodByRecipeId(foodId,recipeId);
            RecipeVo recipe1 = recipeService.SelectRecipeVoByRecipeId(recipeId);
            result.setCode(200);
            result.setData(recipe1);
            result.setMsg("添加成功");
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode(201);
            result.setMsg("系统繁忙,请稍后再试");
        }
        return result;
    }
    @PostMapping("/recipe/addRecipe")
    public ResponseResult<Void> addRecipe(@RequestBody RecipeVo recipe) {
        ResponseResult<Void> result = new ResponseResult<>();
        try {
            recipeService.addRecipe(recipe);
            result.setCode(200);
            result.setMsg("添加成功");
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode(201);
            result.setMsg("系统繁忙,请稍后再试");
        }
        return result;
    }
    @PutMapping("/recipe/modifyStatusById/{id}")
    public ResponseResult<Void> modifyStatusById(@PathVariable("id") Integer id) {
        ResponseResult<Void> result = new ResponseResult<>();
        try {
            recipeService.modifyStatusById(id);
            result.setCode(200);
            result.setMsg("发布成功");
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode(201);
            result.setMsg("系统繁忙,请稍后再试");
        }
        return result;
    }
}
